package cn.iocoder.yudao.module.resource.controller.admin.usertimetransaction;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.resource.controller.admin.usertimetransaction.vo.UserTimeTransactionPageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.usertimetransaction.vo.UserTimeTransactionRespVO;
import cn.iocoder.yudao.module.resource.dal.dataobject.usertimetransaction.UserTimeTransactionDO;
import cn.iocoder.yudao.module.resource.service.usertimetransaction.UserTimeTransactionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 用户时长交易记录")
@RestController
@RequestMapping("/lm/user-time-transaction")
@Validated
public class UserTimeTransactionController {

    @Resource
    private UserTimeTransactionService userTimeTransactionService;

    @GetMapping("/get")
    @Operation(summary = "获得用户时长交易记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('lm:user-time-transaction:query')")
    public CommonResult<UserTimeTransactionRespVO> getUserTimeTransaction(@RequestParam("id") String id) {
        UserTimeTransactionDO userTimeTransaction = userTimeTransactionService.getUserTimeTransaction(id);
        return success(BeanUtils.toBean(userTimeTransaction, UserTimeTransactionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户时长交易记录分页")
    @PreAuthorize("@ss.hasPermission('lm:user-time-transaction:query')")
    public CommonResult<PageResult<UserTimeTransactionRespVO>> getUserTimeTransactionPage(@Valid UserTimeTransactionPageReqVO pageReqVO) {
        PageResult<UserTimeTransactionDO> pageResult = userTimeTransactionService.getUserTimeTransactionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserTimeTransactionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户时长交易记录 Excel")
    @PreAuthorize("@ss.hasPermission('lm:user-time-transaction:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserTimeTransactionExcel(@Valid UserTimeTransactionPageReqVO pageReqVO,
                                               HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserTimeTransactionDO> list = userTimeTransactionService.getUserTimeTransactionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户时长交易记录.xls", "数据", UserTimeTransactionRespVO.class,
                BeanUtils.toBean(list, UserTimeTransactionRespVO.class));
    }

}